package igloo.model;

import igloo.aws.Region;
import igloo.util.EventSupport;
import igloo.util.IEventRaiser;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.ListMultimap;

public class Igloo implements IEventRaiser<IIglooListener>
{
    private final EventSupport<IIglooListener> mEventSupport = new EventSupport<IIglooListener>();
    private final ListMultimap<Region, Vault> mVaults = LinkedListMultimap.create();
    private final List<Job> mJobs = new LinkedList<Job>();

    public Igloo()
    {
    }

    public List<Vault> getVaults(Region region)
    {
        List<Vault> vaults = mVaults.get(region);
        if(vaults == null)
            vaults = Collections.emptyList();

        return vaults;
    }

    public void addVault(Vault vault)
    {
        mVaults.put(vault.getRegion(), vault);

        for(IIglooListener l : mEventSupport.getListeners())
            l.vaultAdded(vault);
    }

    public void removeVault(Vault vault)
    {
        mVaults.remove(vault.getRegion(), vault);

        for(IIglooListener l : mEventSupport.getListeners())
            l.vaultRemoved(vault);
    }

    public void addJob(Job job)
    {
        mJobs.add(job);
    }

    public void removeJob(Job job)
    {
        mJobs.remove(job);
    }

    @Override
    public void addListener(IIglooListener listener)
    {
        mEventSupport.addListener(listener);
    }

    @Override
    public void removeListener(IIglooListener listener)
    {
        mEventSupport.removeListener(listener);
    }
}
